package com.xbfinance.manager.web;

import com.xbfinance.manager.pojo.po.P2pProjectOrder;
import com.xbfinance.manager.pojo.po.P2pUserOrder;
import com.xbfinance.manager.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @RequestMapping(value = "/editOrderPage", method = RequestMethod.GET)
    @ResponseBody
    public P2pProjectOrder editOrderPage(Integer oid){

        P2pProjectOrder order =null;

        try {

            order =  orderService.selectOrderByOid(oid);

        } catch (Exception e) {
            e.printStackTrace();

        }
        return order;
    }

    @RequestMapping(value = "/deleteOrder", method = RequestMethod.DELETE)
    @ResponseBody
    public Map<String,Object> deleteOrder(Integer oid){

        Map<String,Object> map = new HashMap<>();

        try {
            int result = orderService.deleteOrder(oid);

            if(result!=0){
                map.put("flag",true);
            }else{
                map.put("flag",false);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return map;
    }

    @RequestMapping(value = "/deleteBatch", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> deleteBatch(@RequestParam(value = "ids[]") List<Integer> ids){

        Map<String,Object> map = new HashMap<>();
        int i=0;
        try {
            i = orderService.deleteBatch(ids);

            if(i!=0){
                map.put("flag",true);
            }else{
                map.put("flag",false);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return map;
    }

    @RequestMapping(value = "/queryOrder", method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> queryOrder(Integer page,Integer rows,Integer queryOid,Long queryProjectid){

        Map<String,Object> map = new HashMap<>();

        try {

            List<P2pUserOrder> list = orderService.queryOrder(page,rows,queryOid,queryProjectid);
            int total = orderService.queryCount(queryOid,queryProjectid);

            map.put("list",list);
            map.put("total",total);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return map;
    }

    @RequestMapping(value = "/editOrder", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> editOrder(Integer oid, P2pProjectOrder order){

        Map<String,Object> map = new HashMap<>();

        try{

            int result = orderService.editOrder(oid,order);

            if(result!=0){
                map.put("flag",true);
            }else {
                map.put("flag",false);
            }

        }catch (Exception e){
            e.printStackTrace();
        }

        return map;
    }

}
